<%@ page contentType="text/plain;charset=UTF-8"%>

<%@ page import="java.io.File"%>
<%@ page import="java.text.DateFormat"%>
<%@ page import="java.text.SimpleDateFormat"%>
<%@ page import="org.apache.commons.codec.binary.Base64"%>

<%
  // byte[] bytes = Base64.getDecoder().decode(request.getParameter("path")); // use jdk 1.8 and faster
  byte[] bytes = new Base64().decodeBase64(request.getParameter("path"));
  String path = new String(bytes, "UTF-8");
  System.out.println("Browsing " + path);
  File currDir = new File(path);
  File[] files = currDir.listFiles();
%>

    <div>Current Path: <%=getFullPath(currDir)%></div>
    <div>File(s) in Directory:</div>
    <div>
      <%
        if (files != null && files.length > 0) {
      %>
      <table class="list">
        <colgroup>
          <col width="5%"/>
          <col width="35%"/>
          <col width="12%"/>
          <col width="16%"/>
          <col width="12%"/>
          <col width="20%"/>
        </colgroup>
        <tr>
          <th>S/N</th>
          <th>Name</th>
          <th>Type</th>
          <th>Last Modified</th>
          <th>Size</th>
          <th>Permission</th>
        </tr>
      <%
            DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
            for (int i = 0; i < files.length; i++) {
                File file = files[i];
                String name = file.getName();
                String type = file.isDirectory() ? "Directory" : "File";
                double size = file.length() / 1024.0;
                size = Math.round(size * 100.0) / 100.0;

                String permissions = (file.canRead() ? "R" : "") + (file.canWrite() ? "W" : "");

                String dirPath = file.getCanonicalPath();
                // String encodedPath = Base64.getEncoder().encodeToString(dirPath.getBytes("UTF-8"));  // use jdk 1.8 and faster
                String encodedPath = new Base64().encodeBase64String(dirPath.getBytes("UTF-8"));
      %>
        <tr>
          <td class="set"><%=i + 1%></td>
          <td><%if (file.isDirectory()) {%><a href="javascript:browseDir('<%=encodedPath%>');"><%} else {%><a href="downloadHandler.jsp?p=<%=encodedPath%>" target="_blank"><%}%><%=name%></a></td>
          <td class="set"><%=type%></td>
          <td class="num"><%=df.format(file.lastModified())%></td>
          <td class="num"><%=size%> KB</td>
          <td class="set"><%=permissions%></td>
        </tr>
      <%
            }
      %>
      </table>
      <%
        } else {
      %>
      Empty Folder.
      <%
        }
      %>
    </div>

<%!
  public String getFullPath(File currFile) throws Exception {
      String fullPath = "";
      if (currFile != null) {
          Base64 base64 = new Base64();
          String tmpPath = "";
          File parentFile = currFile.getParentFile();
          while (parentFile != null && !"".equals(parentFile.getName())) {
              String dirPath = parentFile.getCanonicalPath();
              // String encodedPath = Base64.getEncoder().encodeToString(dirPath.getBytes("UTF-8"));  // use jdk 1.8 and faster
              String encodedPath = base64.encodeBase64String(dirPath.getBytes("UTF-8"));
              tmpPath = "<a href=\"javascript:browseDir('" + encodedPath + "');\">" + parentFile.getName() + "</a>" + File.separator + tmpPath;
              parentFile = parentFile.getParentFile();
          }
          // current is root of path
          if (parentFile == null) {
              fullPath = currFile.getCanonicalPath();
          } else {
              fullPath = parentFile.getCanonicalPath();
          }
          fullPath = "<a href=\"javascript:browseDir('"
                    + base64.encodeBase64String(fullPath.getBytes("UTF-8")) + "');\">"
                    + fullPath.substring(0, fullPath.length() - 1)
                    + "</a>" + File.separator;
          fullPath = fullPath + tmpPath + currFile.getName();
      }

      return fullPath;
  }
%>